package com.ruoyi.sgsc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sgsc.domain.products;
import com.ruoyi.sgsc.service.IproductsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author wy
 * @date 2024-09-29
 */
@Anonymous
@RestController
@RequestMapping("/sgsc/products")
public class productsController extends BaseController
{
    @Autowired
    private IproductsService productsService;

    /**
     * 查询商品列表
     */

    @GetMapping("/list")
    public TableDataInfo list(products products)
    {
//        startPage();
        List<products> list = productsService.selectproductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */

    @PostMapping("/export")
    public void export(HttpServletResponse response, products products)
    {
        List<products> list = productsService.selectproductsList(products);
        ExcelUtil<products> util = new ExcelUtil<products>(products.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */

    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productsService.selectproductsByProductId(productId));
    }

    /**
     * 根据多条商品id获取对应商品详情
     * @param productIds
     * @return
     */
    @GetMapping(value = "/productlist/{productIds}")
    public AjaxResult getInfo(@PathVariable("productIds") Long[] productIds)
    {
        return success(productsService.selectproductsByProductIds(productIds));
    }


    /**
     * 获取分类id获取详细信息
     */
    @GetMapping(value = "/category/{categoryId}")
    public AjaxResult getInfoByCategoryId(@PathVariable("categoryId") Long categoryId)
    {
        return success(productsService.selectproductsByCategoryId(categoryId));
    }

    /**
     * 获取商品默认信息
     * @param isActive
     * @return
     */
    @GetMapping(value = "/isActive/{isActive}")
    public AjaxResult getInfoByIsActive(@PathVariable("isActive") Integer isActive)
    {
        return success(productsService.selectproductsByIsActive(isActive));
    }


    /**
     * 新增商品
     */

    @PostMapping
    public AjaxResult add(@RequestBody products products)
    {
        return toAjax(productsService.insertproducts(products));
    }

    /**
     * 修改商品
     */

    @PutMapping
    public AjaxResult edit(@RequestBody products products)
    {
        return toAjax(productsService.updateproducts(products));
    }

    /**
     * 删除商品
     */

	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productsService.deleteproductsByProductIds(productIds));
    }

}
